package com.guo.core.framework.suanfa;

import cn.hutool.core.map.MapUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * @projectName: 接口梳理
 * @package: com.guo.core.framework.suanfa
 * @className: Test
 * @author: zhangcan30
 * @description: TODO
 * @date: 2023/5/6 19:01
 * @version: 1.0
 */
public class pushTSS {


    public static void main(String[] args) throws InterruptedException {
        String fileName = "C:/Users/zhangcan30/Desktop/TSS.xlsx";
        ExcelReader reader = ExcelUtil.getReader(fileName, 0);
        List<Map<String, Object>> readAll = reader.readAll();


        for (int i = 0; i < readAll.size(); i++) {
            Map<String, Object> map = readAll.get(i);
            String taskNo = MapUtil.getStr(map, "业务单号");
            System.out.println((i + 1) + "+====:" + taskNo);

            String urlParameters1 = "{\n" +
                "  \"id\": 85593,\n" +
                "  \"executorParam\": \"{\\n    \\\"taskNo\\\": \\\"" + taskNo + "\\\"\\n}\",\n" +
                "  \"cron\": \"45 0/2 * * * ?\",\n" +
                "  \"systemId\": 800\n" +
                "}";

            String urlParameters2 = "{\n" +
                "  \"id\": 85594,\n" +
                "  \"executorParam\": \"{\\n    \\\"taskNo\\\": \\\"" + taskNo + "\\\"\\n}\",\n" +
                "  \"cron\": \"45 0/2 * * * ?\",\n" +
                "  \"systemId\": 800\n" +
                "}";

            sendHttpMsg(taskNo,urlParameters1);
            sendHttpMsg(taskNo,urlParameters2);
            Thread.sleep(3000);

        }
//        sendHttpMsg("TK1887FBC00BC1807124899");
    }

    private static void sendHttpMsg(String taskNo,String urlParameters) {
        try {
            // 创建URL对象
            URL url = new URL("https://apiprod.midea.com/djs/djs-dispatch-center/jobs/trigger");

            // 创建HttpURLConnection对象
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setDoOutput(true);
            // 设置请求方法为GET
            con.setRequestMethod("POST");

            con.setRequestProperty("IDaaS-Auth-Token", "eyJraWQiOiJBRVMiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJtZWlDbG91ZC5jb20iLCJhdWQiOiIiLCJleHAiOjE3MDU0OTAyNDIsImp0aSI6IndjbEE5TldLV0VIMjZLZEFDaGg1dVEiLCJpYXQiOjE3MDU0NTQyNDIsIm5iZiI6MTcwNTQ1NDEyMiwic3ViIjoiYjhjMGM1NWYyMTQ4NDc5OGJkZjFmYzViNjU5N2RmOWYiLCJ1dWlkIjoiTUMyMjM1OTAxMzAifQ.oqJ5QFF1FAdBt-WJeXAAUXIKYvjVXlj2brA0R4EsZzo");
            con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            con.setRequestProperty("Connection", "keep-alive/json");


            OutputStream outputStream = con.getOutputStream();


            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();

            // 获取响应状态码
            int status = con.getResponseCode();

            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            System.out.println("Response status: " + status);
            System.out.println("Response body: " + response.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
